﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using PepsiCo.Business;
using System.Configuration;

namespace PepsiCo.UI
{
    public class BasePage : System.Web.UI.Page
    {
        #region Properties
        private PepsiCoDBDataContext db = new PepsiCoDBDataContext(LocalSetting.ConnectionString);
        public PepsiCoDBDataContext DB
        {
            get { return db; }
        }
        public pepsi_User CurUser
        {
            get
            {
                if (Session[_SESSION.USER_NAME] == null) return null;
                return (pepsi_User)Session[_SESSION.USER_NAME];
            }
            set
            {
                if (Session[_SESSION.USER_NAME] == null)
                    Session.Add(_SESSION.USER_NAME, value);
                else Session[_SESSION.USER_NAME] = value;
            }
        }
        public string AccessDeniedURL
        {
            get { return ConfigurationManager.AppSettings["AccessDeniedURL"]; }
        }
        public string DefaultURL
        {
            get { return ConfigurationManager.AppSettings["DefaultURL"]; }
        }
        public string LoginURL
        {
            get { return ConfigurationManager.AppSettings["LoginURL"]; }
        }
        public string ReturnURL
        {
            get
            {
                string returnURL = (string)Session[_SESSION.RETURN_URL];
                if (returnURL == null || returnURL == AccessDeniedURL || returnURL == LoginURL) return DefaultURL;
                return returnURL;
            }
            set
            {
                if (Session[_SESSION.RETURN_URL] == null)
                    Session.Add(_SESSION.RETURN_URL, value);
                else
                    Session[_SESSION.RETURN_URL] = value;
            }
        }
        #endregion
       
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (RequiredLogin())
            {
                ReturnURL = Request.RawUrl;
                RedirectToPage(LoginURL);
            }
        }
       
        protected virtual bool RequiredLogin()
        {
            if (Session[_SESSION.USER_NAME] == null)
                return true;
            return false;
        }

        /// <summary>
        /// Điều hướng trang
        /// </summary>
        /// <param name="urlPage">Địa chỉ điều hướng</param>
        protected void RedirectToPage(string urlPage)
        {
            Response.Redirect(urlPage);
        }

        protected string GetQueryString(string parameter)
        {
            if (Request.QueryString[parameter] == null) return string.Empty;
            return (string)Request.QueryString[parameter];
        }

        /// <summary>
        /// Mã hóa MD5 một xâu ký tự
        /// </summary>
        /// <param name="input">Xâu ký tự cần mã hóa</param>
        /// <returns></returns>
        public string EncryptMD5(string input)
        {
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5 = new MD5CryptoServiceProvider();
            originalBytes = UTF8Encoding.Default.GetBytes(input);
            encodedBytes = md5.ComputeHash(originalBytes);
            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes);
        }

        protected virtual bool AllowSendMessage()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                                       (p.pepsi_Role.RoleName == Role.SendMessage || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowCreateUser()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.CreateUser || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowDeleteUser()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.DeleteUser || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowCreateUnit()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.CreateUnit || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowDeleteUnit()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.DeleteUnit || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowCreateSale()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.CreateSale || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowDeleteSale()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.DeleteSale || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowGrantPrivileges()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.GrantPrivileges || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowEditUser()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.EditUser || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowEditUnit()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.EditUnit || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowEditSale()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.EditSale || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }

        protected virtual bool AllowEditFunction()
        {
            if (CurUser == null)
                return false;
            if (DB.pepsi_User_Roles.Any(p => p.UserID == CurUser.UserID &&
                           (p.pepsi_Role.RoleName == Role.EditFunction || p.pepsi_Role.RoleName == Role.Admin)))
                return true;
            return false;
        }
    }


    public class _SESSION
    {
        public const string USER_NAME = "USERNAME";
        public const string RETURN_URL = "ReturnURL";
    }

    public class Role
    {
        public const string Admin = "Admin";
        public const string SendMessage = "SendMessage";
        public const string CreateUser = "CreateUser";
        public const string DeleteUser = "DeleteUser";
        public const string CreateUnit = "CreateUnit";
        public const string DeleteUnit = "DeleteUnit";
        public const string CreateSale = "CreateSale";
        public const string DeleteSale = "DeleteSale";
        public const string GrantPrivileges = "GrantPrivileges";
        public const string EditUser = "EditUser";
        public const string EditUnit = "EditUnit";
        public const string EditSale = "EditSale";
        public const string EditFunction = "EditFunction";
    }
}